//
//  LicensedMyVC.m
//  Doctors
//
//  Created by 王森 on 16/4/13.
//  Copyright © 2016年 王森. All rights reserved.
//

#import "LicensedMyVC.h"
#import "MyMassageTableViewCell.h"
#import "LicensendincomeViewController.h"
#import "ChangePasswordViewController.h"
#import "LoginViewController.h"

@interface LicensedMyVC ()

@end

@implementation LicensedMyVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"我的";
    
    NSArray *titleArray=@[@"我的收入",@"修改密码",@"版本信息",@"退出登录"];
    
    NSArray *imgAray=@[@"wodeshour",@"icon_xiugaimima",@"icon_banbenxinxi",@"icon_tuichudenglu"];
    
    for (int i=0; i<titleArray.count; i++) {
        
        NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:titleArray[i],@"title", imgAray[i],@"imgName",nil];
        
        [self.dataArray addObject:dic];
        
    }
    
    
    [self initWithTableViewNib:@"MyMassageTableViewCell" didSelectCellBlock:^(NSIndexPath *indexPath) {
            NSLog(@"....");
            if (indexPath.row==0) {
                ALLOC(LicensendincomeViewController, view);
                
                [self pushNextViewController:view];
            }
        
        if (indexPath.row==1) {
            ALLOC(ChangePasswordViewController, view);
            [self pushNextViewController:view];
            
        }
        
        if(indexPath.row==3)
        {
            
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                                message:@"是否退出"
                                                               delegate:self
                                                      cancelButtonTitle:@"取消"
                                                      otherButtonTitles:@"确定", nil];
            [alertView show];
        }
    
        
    }];
    

    
//    [self initWithTableViewNib:@"MyMassageTableViewCell"];
  
//    
//    __unsafe_unretained __typeof(self) weakSelf = self;
//    
//    [self requestApiWithUrl:nil dictionary:nil isCache:YES tableViewHeaderFresh:^{
//        weakSelf.page=1;
//        
//        
//    } success:^(id responseObject) {
//        
//    } faild:^(NSError *error) {
//        
//    }];
    // Do any additional setup after loading the view from its nib.
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex==1) {
        ALLOC(LoginViewController, login);
        login.hidesBottomBarWhenPushed=YES;
        login.isFromMassageViewCOntroller=YES;
        [[TMCache sharedCache]removeAllObjects];
        [self clearCacheFiles];
        [self presentViewController:login animated:YES completion:nil];
    }
}
/*!
 *  @author WS, 16-01-15 18:01:25
 *
 *  退出登录清除缓存
 */
-(void)clearCacheFiles
{
    //清楚缓存
    dispatch_async(
                   
                   dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)
                   
                   , ^{
                       NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES)objectAtIndex:0];
                       
                       NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                       
                       NSLog(@"files :%lu",(unsigned long)[files count]);
                       
                       for (NSString *p in files) {
                           //如有需要，加入条件，过滤掉不想删除的文件
                           
                           NSError *error;
                           
                           NSString *path = [cachPath stringByAppendingPathComponent:p];
                           
                           if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                               
                               [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                               
                           }
                           
                       }
                       
                       
                       
                   });
    
    
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    __weak typeof(self)weakSelf=self;
    //
    //    OrderModel *model=self.dataArray[indexPath.row];
    
    MyMassageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyMassageTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    if (indexPath.row==2) {
        cell.subTitle.text=@"v1.0";
        cell.jianTouImg.hidden=YES;
        cell.subTitle.hidden=NO;

    }else
    {
        cell.subTitle.hidden=YES;
        cell.jianTouImg.hidden=NO;

    }
    cell.title.text=self.dataArray[indexPath.row][@"title"];
    
    NSString *str=self.dataArray[indexPath.row][@"imgName"];
    
    cell.headerImg.image=[UIImage imageNamed:[NSString stringWithFormat:@"%@",str]];
    
    
    
    
    //    [cell cellFuzhi:self.dataArray[indexPath.row]];
    
    //    __weak typeof(self)weakSef=self;
    
    
    
    return cell;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    return 10;
    
    return self.dataArray.count;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 46;
    
    
}
//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    
//    
//    NSLog(@"请重写方法");
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
